$(function(){
	$('#tt').datagrid({
		url:'/dataInc/getDataIncListPage'
	});
	
});

function doAdd(){
	$('#addForm').form('clear');
	$('#incTypeId').combobox('setValue',1);
	$('#addWin').window('open');
}

function doEdit(){
	var record = $('#tt').datagrid('getSelected');
	if(record==null||record==''){
		$.messager.alert("提示", "请先选择一条记录");
		return;
	}
	
	$('#idId').val(record.id);
	$('#incAliasId').val(record.incAlias);
	$('#incTypeId').combobox('setValue',record.incType);
	$('#incNameId').val(record.incName);
	$('#incValueId').val(record.incValue);
	$('#startDayId').val(record.startDay);
	$('#intervalTimeId').val(record.intervalTime);
	
	$('#sourceSqlId').val(record.sourceSql);
	$('#targetSqlId').val(record.targetSql);
	$('#mappingStrId').val(record.mappingStr);
	
	$('#addWin').window('open');
}

function updateStatus(status){
	var record = $('#tt').datagrid('getSelected');
	if(record==null||record==''){
		$.messager.alert("提示", "请先选择一条记录");
		return;
	}
	
	var dataIncId=record.id;
	$.ajax({
		url: '/dataInc/updateDataIncStatus',
		type: 'POST',
		data : {
            id:dataIncId,
            status:status
		},
		success : function(data) {
			if (data.return_code == '1') {
				$.messager.alert(market.content.title, "操作成功");
				$("#tt").datagrid('reload');
				$('#tt').datagrid('unselectAll');
			} else {
				$.messager.alert(market.content.title, data.return_msg);
			}
		},
		error : function(e) {
			$.messager.alert("未知错误");
		}
	})
}

function doSearch(){
	$('#tt').datagrid({
		url:'/dataInc/getDataIncListPage',
		queryParams:{
		}
	});
	$('#tt').datagrid('unselectAll');
}

function save(){
	var id=$('#idId').val();
	var url='/dataInc/updateDataInc';
	if(id==null||id==''){
		url='/dataInc/saveDataInc';
	}else{
		var record = $('#tt').datagrid('getSelected');
		if(record==null||record==''){
			$.messager.alert("提示", "请先选择一条记录");
			return;
		}
		if(record.status=='1'){
			$.messager.alert("提示", "开启状态的记录不允许修改");
			return;
		}
	}
	
	
	
	
	$('#addForm').form('submit',{
		url : url,
		onSubmit : function() {
			var flag = $(this).form('validate');
			if (!flag) {
				return flag;
			}
			showProcess(true, market.content.title, market.content.submitting);
			return true;
		},
		success : function(data) {
			showProcess(false);
			var data = eval('(' + data + ')');
			if (data.return_code == '1') {
				$("#addWin").window('close');
				$('#tt').datagrid('reload');
				$('#tt').datagrid('unselectAll');
			} else {
				$.messager.alert(market.content.title, data.return_msg);
			}
		},
		onLoadError : function() {
			showProcess(false);
			$.messager.alert(market.content.title, market.content.networkError);
		}
	});
	
}

function cancel(){
	$('#addWin').window('close');
}

function statusFormatter(value){
	if(value=='1'){
		return '启动';
	}else{
		return '关闭';
	}
}

function incTypeFormatter(value){
	if(value=='1'){
		return 'int型增量';
	}else if(value=='2'){
		return '时间增量';
	}else{
		return '未知';
	}
}
